from database import db
from ..models.order import Order
from ..models.member import Member


class MemberService(object):
    @classmethod
    def create_member(cls, id, screen_name, head_image_id, *args, **kwargs):
        kwargs.update({
            'id': id,
            'screen_name': screen_name,
            'head_image_id': head_image_id,
        })
        member = Member(**kwargs)
        db.session.add(member)

    @classmethod
    def get_member_by_id(cls,id):
        return Member.query.filter(Member.id==id).first()

    @classmethod
    def get_orders_by_member(cls, member_id, pagination=None):
        query = Order.query.filter(Order.member_id == member_id).all()

        if pagination:
            page_size = pagination.get('page_size', 10)
            page = pagination.get('current_page', 1)

            pagination = query.paginate(page, page_size, False)
            return pagination.items, pagination
        return query, None